<template>
  <div class="container">
    <div class="header">
      <van-icon
        @click="goBack"
        name="arrow-left"
        class="header-left"
        size="20"
      />
      填写服务订单
    </div>
    <status-bar item="0"></status-bar>
    <van-cell class="cell">
      <template #title>
        <van-image
          :src="createInfo.service.serviceImg"
          width="25"
          height="25"
        />
        <span class="server-name">{{ createInfo.service.serviceName }}</span>
      </template>
      <template #default>
        <div class="server-text"><van-icon name="warning" />服务内容</div>
      </template>
    </van-cell>
    <!-- form表单 -->
    <van-cell-group class="cell">
      <van-cell>
        <template #title>就诊医院 </template>
        <template #default>
          <div class="server-text" @click="showHospical = true">
            {{ form.hospital_name || "请选择就诊医院" }}
            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>
      <!-- 就诊机构 -->
      <van-cell>
        <template #title>就诊时间 </template>
        <template #default>
          <div class="server-text" @click="showStartTime = true">
            {{ currentDate || "请选择就诊时间" }}
            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>
      <!-- 陪诊师 -->
      <van-cell>
        <template #title>陪诊师 </template>
        <template #default>
          <div @click="showComponion = true">
            {{ companionName || "请选择陪护师" }}
            <van-icon name="arrow" />
          </div>
        </template>
      </van-cell>
      <!-- 接送地址 -->
      <van-cell>
        <template #title>接送地址 </template>
        <template #default>
          <van-field
            class="text"
            input-align="right"
            v-model="form.receiveAddress"
            placeholder="请填写接送地址"
          />
        </template>
      </van-cell>
      <!-- 联系电话 -->
      <van-cell>
        <template #title>联系电话 </template>
        <template #default>
          <van-field
            class="text"
            input-align="right"
            v-model="form.tel"
            placeholder="请填写联系电话"
          />
        </template>
      </van-cell>
    </van-cell-group>
    <van-cell-group title="服务需求" class="cell">
      <van-field
        class="text"
        style="height: 100px"
        v-model="form.demand"
        placeholder="请简单描述您要就诊的科室..."
      />
    </van-cell-group>
    <!-- 提交按钮 -->
    <van-button @click="sumbit" class="sumbit" type="primary" size="large"
      >提交订单</van-button
    >
    <van-popup
      position="bottom"
      v-model:show="showHospical"
      :style="{ height: '30%' }"
    >
      <van-picker
        :columns="showHospicalColumns"
        @confirm="showHospConfirm"
        @cancel="showHospical = false"
        @change="onChange"
    /></van-popup>
    <van-popup
      position="bottom"
      v-model:show="showStartTime"
      :style="{ height: '30%' }"
    >
      <van-date-picker
        @confirm="showTimeConfirm"
        @cancel="showStartTime = false"
        title="选择日期"
        :min-date="minDate"
      />
    </van-popup>
    <van-popup
      position="bottom"
      v-model:show="showComponion"
      :style="{ height: '30%' }"
    >
      <van-picker
        :columns="compontionColumns"
        @confirm="showComponionConfirm"
        @cancel="showComponion = false"
      />
    </van-popup>
    <!-- 支付二维码弹窗 -->
    <van-dialog :showConfirmButton="true" v-model:show="showCode">
      <van-icon name="cross" class="close" @click="closeCode" />
      <div>微信支付</div>
      <van-image width="150" height="150" :src="codeImg" />
      <div>请使用本人微信扫描二维码</div>
    </van-dialog>
  </div>
</template>
<script setup>
import { useRouter } from "vue-router";
import { getCurrentInstance, onMounted, reactive, ref, computed } from "vue";
import StatusBar from "../../components/statusBar.vue";
const { proxy } = getCurrentInstance();
const router = useRouter();
const createInfo = reactive({
  companion: [],
  hospitals: [],
  service: [],
});
import Qrcode from "qrcode";
// form 数据
const form = reactive({});
const showHospicalColumns = computed(() => {
  return createInfo.hospitals.map((item) => {
    return { text: item.name, value: item.id };
  });
});
// 选择医院
const showHospConfirm = (item) => {
  form.hospital_id = item.selectedOptions[0].value;
  form.hospital_name = item.selectedOptions[0].text;
  showHospical.value = false;
};
const goBack = () => {
  router.go(-1);
};
onMounted(async () => {
  const { data } = await proxy.$api.h5Compainon();
  Object.assign(createInfo, data);
});
// 就诊医院
const showHospical = ref(false);
// 选择就诊时间
const showStartTime = ref(false);
const currentDate = ref();
// 最小日期
const minDate = ref(new Date());
const showTimeConfirm = (item) => {
  const dateStr = item.selectedValues.join("-");
  form.starttime = new Date(dateStr).getTime();
  showStartTime.value = false;
  currentDate.value = dateStr;
};
// 选择陪诊师
const companionName = ref();
const showComponion = ref(false);
const showComponionConfirm = (item) => {
  form.companion_id = item.selectedOptions[0].value;
  companionName.value = item.selectedOptions[0].text;
  showComponion.value = false;
};
const compontionColumns = computed(() => {
  return createInfo.companion.map((item) => {
    return { text: item.name, value: item.id };
  });
});
// 支付弹窗
const showCode = ref(false);
const codeImg = ref();
const closeCode = () => {
  showCode.value = false;
  router.push("/order");
};
const sumbit = async () => {
  const params = [
    "hospital_id",
    "hospital_name",
    "demand",
    "companion_id",
    "receiveAddress",
    "tel",
    "starttime",
  ];
  for (const i of params) {
    if (!form[i]) {
      showNotify({ message: "请把每一项都填写" });
      //   return;
    }
  }

  const data = await proxy.$api.createOrder(form);
  Qrcode.toDataURL(data.data.wx_code).then((url) => {
    showCode.value = true;
    codeImg.value = url;
  });
};
</script>
<style scoped lang="less">
.container {
  background-color: #f0f0f0;
  height: 100vh;
}
.header {
  padding: 10px 0;
  text-align: center;
  line-height: 30px;
  font-size: 15px;
  .header-left {
    float: left;
  }
}

.cell {
  width: 95%;
  margin: 5px auto;
  background-color: #fff;
  ::v-deep(.van-field__control) {
    color: var(--van-cell-value-color);
  }
  ::v-deep(.van-cell__title) {
    display: flex;
    align-items: center;
  }
  .server-name {
    margin-left: 10px;
  }
}
.service-text {
  background: url()
    no-repeat center center;
  background-size: 20px;
}
.sumbit {
  position: absolute;
  bottom: 0;
}
::v-deep(.van-dialog__content) {
  text-align: center;
  padding: 20px;
  .close {
    position: absolute;
    left: 20px;
  }
}
</style>
